#!/bin/bash
# version: V1.0.1 -- 202111251130
longtime=$(date +%Y%m%d%H%M)
echo "[${longtime}] reset mysql server start"
CONF=$1

function clean_file() {
    ## 读取初始化变量
    source init_variable
    # 检查变量,写入默认值
    source check_var
    checkAndSetVar
     # 清除 mysql 目录
    if [ -d ${basedir} ]; then
        echo "[${longtime}] find the dir ${basedir} and remove... "
        rm -rf ${basedir}
    fi


}

function reset() {
    if [[ ${SYSTEMCTL_TYPE} == 1 ]];then
        systemctl stop mysqld
        systemctl disable mysqld
        rm -rf /etc/systemd/system/mysqld.server
        rm -rf /usr/lib/systemd/system/mysqld.server

    elif [[ ${SYSTEMCTL_TYPE} == 2 ]]; then
        service stop mysqld
        chkconfig mysqld off
        rm /etc/init.d/mysqld
    fi

    ## 读取初始化变量
    source init_variable
    # 检查变量,写入默认值
    source check_var
    checkAndSetVar
    # 清除 mysql data 目录
    if [ -d ${datadir} ]; then
        echo "[${longtime}] find the dir ${datadir} and remove... "
        rm -rf ${datadir}
    fi

    PIDS=$(ps -ef|grep mysqld|grep -v grep|awk '{print $2}')
    for i in ${PIDS};do
        echo "kill pid $i"
        kill $i
    done
    ./mysql_clean_sock.sh $CONF

    echo "[${longtime}] reset mysql server end"

}
reset

